<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
<link rel="stylesheet" href="/static/css/style.css">

</head>
<body>
	
	<div id="header">
			<img class="logo_img" alt="" src="/static/img/logo.gif" >
			<span class="wel_word">购物车</span>
			{{if .Userid}}
			<div>
				<span>欢迎<span class="um_span">{{.Username}}</span>光临尚硅谷书城</span>
				<a href="/toCart">购物车</a>
				<a href="/toMyOrders">我的订单</a>
				<a href="/logout">注销</a>&nbsp;&nbsp;
				<a href="/main">返回</a>
			</div>
			{{else}}
			<div>
				<a href="/login">登录</a> | 
				<a href="/register">注册</a> &nbsp;&nbsp;
				<a href="/toCart">购物车</a>
				<a href="/pages/manager/manager.html">后台管理</a>
				<a href="/main">返回</a>
			</div>
			{{end}}
	</div>
	
	<div id="main">
			{{if .Userid}}

				{{if .Cart.CartItems}}
					<table>
						<tr>
							<td>商品名称</td>
							<td>数量</td>
							<td>单价</td>
							<td>金额</td>
							<td>操作</td>
						</tr>
						
						{{range .Cart.CartItems}}
							<tr>
								<td>{{.Books.Title}}</td>
								<td><input id="{{.Id}}" class="count_input" type="text" value="{{.Count}}" style="width: 20px ; text-align: center;"/></td>
								<td>{{.Books.Price}}</td>
								<td>{{.Amout}}</td>
								<td><a id="{{.Books.Title}}" class="delA" href="/deleteCartItem?ci={{.Id}}">删除</a></td>
							</tr>
						{{end}}
					</table>
			
					<div class="cart_info">
						<span class="cart_span">购物车中共有<span class="b_count">{{.Cart.Tcount}}</span>件商品</span>
						<span class="cart_span">总金额<span class="b_price">{{.Cart.Tamout}}</span>元</span>
						<span class="cart_span"><a  href="/main">继续购物</a></span>
						<span class="cart_span"><a id="cleanCart" href="/emptyCart?cartid={{.Cart.Id}}">清空购物车</a></span>
						<span class="cart_span"><a href="/toCheckOut">去结账</a></span>
					</div>
				{{else}}
			
				<div style="padding-top: 200px;"><h1 align="center">购物车里还没有商品，赶紧买吧！</h1></div>
				{{end}}
			{{else}}
				<div style="padding-top: 200px;"><h1 align="center">您还没有登录，请先登录吧！</h1></div>
				
			{{end}}
		
	</div>
	
	<div id="bottom">
		<span>
			尚硅谷书城.Copyright &copy;2015
		</span>
	</div>
</body>
<script src="/static/script/jquery-1.7.2.js"></script>
<script>
	$(function(){
		$("#cleanCart").click(function(){
			return confirm("您确定要清空购物车吗(╥╯^╰╥)")
		})
		$(".delA").click(function(){
			var bookTitle=$(this).attr("id")
			return confirm("您确定要删除"+bookTitle+"这本书吗")
		})

		$(".count_input").change(function(){
			var count=$(this).val()
			var cid=$(this).attr("id")
			var $Elm=$(this).parent().next().next()
			if (validateNum(count)){
				var url="/toUpdateCartCount"
				var params={"cid":cid,"count":count}
				$.post(url,params,function(res){
					$(".b_count").text(res.tcount)
					$(".b_price").text(res.tamout)
					$Elm.text(res.amout)
				},"json")
			}
			
			
		})

		function validateNum(num){
			var reg=/^[1-9][0-9]+$/gi;
			if(reg.test(num) && num>=0){	
				return true
			}else{
				alert("这个不是一个有效的数字")
				return false
			}
		}
	})
</script>
</html>